package net.wang.concurrency.test.immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import javax.annotation.concurrent.ThreadSafe;

/**
 * 不可变对象
 */
@ThreadSafe
public class ImmutableExample3 {

    private static final ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
    private static final ImmutableSet<Integer> set = ImmutableSet.copyOf(list);
    private static final ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 1, 2, 2, 3, 3);
    private static final ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder().put(1, 1).put(2, 2).build();

    public static void main(String[] args) {
        //不可变集合对象的增加或者修改方法都会抛出异常，查找方法不会异常
        list.add(1);
        set.add(4);
        map.put(1, 2);
        map2.put(1, 2);
    }

}
